package cn.bopomo.dealer.base.action.ajax;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.RequestUtils;

import cn.bopomo.dealer.base.action.BaseAction;
import cn.bopomo.dealer.base.common.ServiceConfig;
import cn.bopomo.dealer.base.service.IOperatorService;
import cn.bopomo.dealer.base.util.ResultCode;
import cn.bopomo.dealer.base.vo.AuthorizeList;
import cn.bopomo.dealer.base.vo.ModuleList;

public class ModifyAuthorizeAjax extends BaseAction {

	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		//AJAX返回设置
		response.setContentType("text/html;charset=UTF-8");
		response.setHeader("Cache-control","no-cache");
		
		StringBuffer result = new StringBuffer();
		AuthorizeList authorize = new AuthorizeList();
		
		String op_result  = ResultCode.OP_FAILED;
		try {
			//获取参数
			Integer mId = new Integer(request.getParameter("moduleRef"));
			ModuleList module = new ModuleList();
			module.setMid(mId);
			//获取对象
			RequestUtils.populate(authorize, request); // 从相应的表单中获取数据(前提:属性名必须一致)
			//AuthorizeList authorize2 = new AuthorizeList();
			//BeanUtils.copyProperties(authorize2,form);
			authorize.setModuleList(module);
			
			//检查对象--"权限"
			if ("".equals(authorize.getAuthorizeAction())){
				op_result = ResultCode.OP_AUTH_ACTION_NULL;
			}else if ("".equals(authorize.getAuthorizeName())){
				op_result =  ResultCode.OP_THE_NAME_EXISTS;
			}else if ("".equals(authorize.getAuthorizeTag())){
				op_result =  ResultCode.OP_THE_TAG_EXISTS;
			}else{
				op_result =  ResultCode.OP_SUCCESS;
			}
			
			//System.out.println(authorize.getAuthorizeName());
			//获取Service
			if (ResultCode.OP_SUCCESS.equals(op_result)){
				IOperatorService os = (IOperatorService)this.findService(ServiceConfig.OPERATOR_SERVICE);
				//修改对象--"权限"
				op_result = os.modifyAuthorize(authorize);
			}
		} catch (Exception e) {
			op_result = ResultCode.OP_FAILED;
			//e.printStackTrace();
			log.error(e);
		}
		//组合返回信息
		result.append(op_result).append("||")
									.append(this.getCodeDescription(op_result,request.getSession().getServletContext().getResource(this.codeDescXMLPath).getPath()));
		//返回 
		response.getWriter().write(result.toString());
		log.info(authorize.getAid() + ":" + authorize.getAuthorizeName() + "  " + result);
		return null;
		
	}
	

}
